package com.waps.demo;

import android.app.Activity;
import android.app.ActivityManager;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.waps.*;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class DemoApp extends Activity implements View.OnClickListener, UpdatePointsNotifier
{

    TextView pointsTextView;
    TextView SDKVersionView;
    int point_total;
    String currency_name;

    String displayText;
    boolean update_text = false;

    final Handler mHandler = new Handler();

    //ads layout.
    LinearLayout adLinearLayout;



    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


        // 激活控制台的日志输出.
        AppLog.enableLogging(true);

        // 连接服务器.  应用启动时调用.
        AppConnect.getInstance(this,"jxg@waps.cn");

        adLinearLayout = (LinearLayout)findViewById(R.id.AdLinearLayout);

        Button offers = (Button) findViewById(R.id.OffersButton);
        Button getPoints = (Button) findViewById(R.id.PointsButton);
        Button displayAd = (Button) findViewById(R.id.DisplayAd);

        offers.setOnClickListener(this);
        getPoints.setOnClickListener(this);
        displayAd.setOnClickListener(this);

        pointsTextView = (TextView) findViewById(R.id.PointsTextView);

        SDKVersionView = (TextView)findViewById(R.id.SDKVersionView);

        SDKVersionView.setText("SDK版本: " + AppConnect.LIBRARY_VERSION_NUMBER);

        //30秒刷新一次
        LinearLayout container =(LinearLayout)findViewById(R.id.AdLinearLayout);
        new AdView(this,container).DisplayAd(30);

    }

    public void onClick(View v)
    {
        if (v instanceof Button)
        {
            int id = ((Button) v).getId();

            switch (id)
            {
                case R.id.OffersButton:
                    // 显示推荐安装程序（Offer）.
                    AppConnect.getInstance(this).showOffers(this);
                    break;

                case R.id.PointsButton:
                    // 从服务器端获取当前用户的积分.
                    // 返回结果在回调函数getUpdatePoints(...)中处理
                    AppConnect.getInstance(this).getPoints(this);
                    break;

            }
        }
    }


    @Override
    protected void onDestroy()
    {
        AppConnect.getInstance(this).finalize();
        super.onDestroy();
    }

    @Override
    protected void onResume() {
        AppConnect.getInstance(this).getPoints(this);
        super.onResume();
    }


    // 创建一个线程
    final Runnable mUpdateResults = new Runnable()
    {
        public void run()
        {
            updateResultsInUi();
        }
    };


    private void updateResultsInUi()
    {
        if (pointsTextView != null)
        {
            if (update_text)
            {
                pointsTextView.setText(displayText);
                update_text = false;
            }
        }
    }




    /**
     * AppConnect.getPoints()方法的实现，必须实现
     * @param currencyName     虚拟货币名称.
     * @param pointTotal     虚拟货币收入总数.
     */
    public void getUpdatePoints(String currencyName, int pointTotal)
    {
        currency_name = currencyName;
        point_total = pointTotal;
        update_text = true;
        displayText = currencyName + ": " + pointTotal;
        mHandler.post(mUpdateResults);
    }




    /**
     * AppConnect.getPoints() 方法的实现，必须实现
     * @param error            请求失败的错误信息
     */

    public void getUpdatePointsFailed(String error)
    {
        update_text = true;
        displayText = "Unable to retrieve tap points from server.";
        mHandler.post(mUpdateResults);
    }




}